//获取应用实例
Page({
  data: {
    addressData: null,
    region: ['广东省', '深圳市', '龙华区'],
    customItem: '全部',
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  bindSave: function (e) {
    var that = this;
    var linkMan = e.detail.value.linkMan;
    var address = e.detail.value.address;
    var mobile = e.detail.value.mobile;

    if (linkMan == "") {
      wx.showModal({
        title: '提示',
        content: '请填写联系人姓名',
        showCancel: false
      })
      return
    }

    if (mobile == "") {
      wx.showModal({
        title: '提示',
        content: '请填写手机号码',
        showCancel: false
      })
      return
    }

    if (address == "") {
      wx.showModal({
        title: '提示',
        content: '请填写详细地址',
        showCancel: false
      })
      return
    }
    var action = "AddShippingAddress";
    var id = that.data.id;
    if (id > 0) {
      action = "UpdateShippingAddress";
    } else {
      id = 0;
    }
    wx.request({
      url: 'https://rjgs.sz81jk.com/api/VshopProcess.ashx',
      method: 'Post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        'action': action,
        'address': address,
        'cellphone': mobile,
        'shipTo': linkMan,
        'pcd': that.data.region,
        'userid': wx.getStorageSync('userid'),
        'shippingid': id
      },
      dataType: 'json',
      success: function (res) {
        console.log(res.data);
        var msg = "添加成功";
        if (id > 0) {
          msg = "修改成功";
        }
        if (res.data.success) {
          wx.showModal({
            title: '提示',
            content: msg,
            showCancel: false,
            success: function (ddd) {
              console.log(res.data.shippingId);
              if (that.data.isOrder) {
                var pages = getCurrentPages(); // 获取页面栈
                var currPage = pages[pages.length - 1]; // 当前页面
                var prevPage = pages[pages.length - 2]; // 上一个页面
                prevPage.setData({
                  id: res.data.shippingId
                });
                wx.navigateBack({
                  delta: 1
                })
              }
              else{
                wx.navigateBack({
                  delta: 1
                })             
              }


            },
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '失败',
            showCancel: false
          })
        }
      }
    })
  },
  onLoad: function (e) {
    var sid = e.id;
    var isOrder = e.isOrder;
    var that = this;
    if (isOrder == "true") {
      that.setData({
        isOrder: true
      })
    } else {
      that.setData({
        isOrder: false
      })
    }
    if (sid > 0) {
      that.setData({
        id: sid
      })
      wx.request({
        url: 'https://rjgs.sz81jk.com/api/VYwbangAPI.ashx',
        method: 'Post',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          'action': 'getAddressById',
          'id': sid
        },
        success: function (res) {
          that.setData({
            addressData: res.data,
          })
          var PCD = that.data.addressData.PCD;
          var arr = PCD.split(',');
          that.setData({
            region: arr
          })
        },
      })
    }
  },
  deleteAddress: function (e) {
    var that = this;
    // var id = e.currentTarget.dataset.id;
    var id = that.data.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除该收货地址吗？',
      success: function (res) {
 if(res.confirm){
   wx.request({
     url: 'https://rjgs.sz81jk.com/api/VshopProcess.ashx',
     method: 'Post',
     header: {
       "Content-Type": "application/x-www-form-urlencoded"
     },
     data: {
       'action': "DelShippingAddress",
       'userid': wx.getStorageSync('userid'),
       'shippingid': id
     },
     dataType: 'json',
     success: function (res) {
       var msg = "删除成功";
       if (res.data.success) {
         wx.showModal({
           title: '提示',
           content: msg,
           showCancel: false,
           success: function (ddd) {
             if (that.data.isOrder) {
               var pages = getCurrentPages(); // 获取页面栈
               var currPage = pages[pages.length - 1]; // 当前页面
               var prevPage = pages[pages.length - 2]; // 上一个页面
               prevPage.setData({
                 id: res.data.shippingId
               });
               wx.navigateBack({
                 delta: 1
               })
             }
             wx.navigateBack({
               delta: 1
             })

           },
         })
       } else {
         wx.showModal({
           title: '提示',
           content: '失败',
           showCancel: false
         })
       }
     }
   })
 }
      }
    })
  },
  bindCancel: function () {
    wx.navigateBack({})
  }
})